package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwbqT;
import cn.e4j.modular.crm.service.impl.GwbqTServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 经销商服务区域控制类
 *
 * @author liuw
 * @since 2019/08/30
 */
@Controller
@RequestMapping("/base/gwai304")
public class Gwai304Controller extends BaseController {

    @Autowired
    private GwbqTServiceImpl gwbqTService;

    /**
     * 跳转页面
     */
    @RequiresPermissions("base:gwai304:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/base/gwai304.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("base:gwai304:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwbqTService.getListData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getUsersList")
    @RequiresPermissions("base:gwai304:init")
    @ResponseBody
    public ResponseData getUsersList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwbqTService.getUsersList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 导出Excel
     */
    @RequestMapping(value="/exportListData",method=RequestMethod.GET)
    @RequiresPermissions("base:gwai304:init")
    public void exportListData(ModelMap map, HttpServletRequest request, HttpServletResponse response){

    }

    /**
     * 添加数据
     */
    @RequestMapping("/addData")
    @RequiresPermissions("base:gwai304:init")
    @ResponseBody
    public ResponseData addData(GwbqT gwbqT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbqT.setGWBQENT(shiroUser.getDBC());
//        GwbqT theGwazT = gwbqTService.getByKey(shiroUser.getDBC(), gwbqT.getGWBQ001(), gwbqT.getGWBQ004());
//        if (theGwazT != null) {
//            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_CODE.getCode(),
//                    "该经销商服务区域已存在");
//        }
        gwbqTService.addData(gwbqT);
        return SUCCESS_TIP;
    }

    /**
     * 修改数据
     */
    @RequestMapping("/editData")
    @RequiresPermissions("base:gwai304:init")
    @ResponseBody
    public ResponseData editData(GwbqT gwbqT) {
        gwbqTService.editData(gwbqT);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/deleteData")
    @RequiresPermissions("base:gwai304:init")
    @ResponseBody
    public ResponseData deleteData(GwbqT gwbqT) {
        gwbqTService.deleteData(gwbqT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("base:gwai304:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwbqT> gwbqTList = inDto.getReqList(GwbqT.class);
        gwbqTService.deleteDataBatch(gwbqTList);
        return SUCCESS_TIP;
    }

}
